<!DOCTYPE HTML>
<script src="../../../resources/js-test.js"></script>
<script src="resources/elementsFromPoint.js"></script>
<style>
html, body {
    margin: 0;
    padding: 0;
}
body {
    height: 500px;
}
</style>
<body>
<div id="host"></div>
<div id="blockHost"></div>
<span id="inlineBlockHost" style="display:inline-block;"></span>
<input type="submit" id="submit">
<div id="console"></div>
<script>
if (window.testRunner)
    testRunner.dumpAsText();

function createBox() {
    var div = document.createElement('div');
    div.style.width = '100px';
    div.style.height = '10px';
    return div;
}

function centerX(element) {
    return element.offsetLeft + element.offsetWidth / 2;
}

function centerY(element) {
    return element.offsetTop + element.offsetHeight / 2;
}

var shadowRoot = host.attachShadow({mode: 'open'});
var box11 = createBox();
var box12 = createBox();
var box13 = createBox();
shadowRoot.appendChild(box11);
shadowRoot.appendChild(box12);
shadowRoot.appendChild(box13);

var nestedHost = document.createElement('div');
var nestedShadowRoot = nestedHost.attachShadow({mode: 'open'});
var box21 = createBox();
var box22 = createBox();
var box23 = createBox();
nestedShadowRoot.appendChild(box21);
nestedShadowRoot.appendChild(box22);
nestedShadowRoot.appendChild(box23);

shadowRoot.appendChild(nestedHost);

var x12 = centerX(box12);
var y12 = centerY(box12);
var x22 = centerX(box22);
var y22 = centerY(box22);

shouldThrow('shadowRoot.elementsFromPoint()');
shouldThrow('shadowRoot.elementsFromPoint(0)');

assertElementsFromPoint(
        'shadowRoot.elementsFromPoint(x12, y12)', [box12, host, document.body, document.documentElement]);
assertElementsFromPoint(
        'nestedShadowRoot.elementsFromPoint(x22, y22)', [box22, nestedHost, host, document.body, document.documentElement]);

assertElementsFromPoint(
        'shadowRoot.elementsFromPoint(x22, y22)', [nestedHost, host, document.body, document.documentElement]);
assertElementsFromPoint(
        'document.elementsFromPoint(x22, y22)',
        [host, document.body, document.documentElement]);

var root3 = blockHost.attachShadow({mode: 'open'});
root3.appendChild(document.createTextNode('text1'));
var root4 = inlineBlockHost.attachShadow({mode: 'open'});
root4.appendChild(document.createTextNode('text2'));

assertElementsFromPoint(
        'root3.elementsFromPoint(centerX(blockHost), centerY(blockHost))',
        [blockHost, document.body, document.documentElement]);
assertElementsFromPoint(
        'document.elementsFromPoint(centerX(blockHost), centerY(blockHost))',
        [blockHost, document.body, document.documentElement]);
assertElementsFromPoint(
        'root4.elementsFromPoint(centerX(inlineBlockHost), centerY(inlineBlockHost))',
        [inlineBlockHost, document.body, document.documentElement]);
assertElementsFromPoint(
        'document.elementsFromPoint(centerX(inlineBlockHost), centerY(inlineBlockHost))',
        [inlineBlockHost, document.body, document.documentElement]);
assertElementsFromPoint(
        'document.elementsFromPoint(centerX(submit), centerY(submit))',
        [submit, document.body, document.documentElement]);
</script>
</body>
